package com.csx.midware.redis;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;

import java.util.List;

public class RedisGeoExample {

    private Jedis jedis;

    public RedisGeoExample() {
        jedis = new Jedis("localhost");
    }

    public void addLocation(String geoKey, String member, double longitude, double latitude) {
        jedis.geoadd(geoKey, longitude, latitude, member);
    }

    public List<GeoRadiusResponse> getNearbyLocations(String geoKey, double longitude, double latitude, double radius, String unit) {
        List<GeoRadiusResponse> results = jedis.georadius(geoKey, longitude, latitude, radius, GeoUnit.valueOf(unit.toUpperCase()));
        return results;
    }

    public static void main(String[] args) {
        RedisGeoExample example = new RedisGeoExample();
        example.addLocation("locations", "Location1", 121.473701, 31.230416);
        example.addLocation("locations", "Location2", 121.473701, 31.230416);

        List<GeoRadiusResponse> nearbyLocations = example.getNearbyLocations("locations", 121.473701, 31.230416, 1000, "m");
        for (GeoRadiusResponse location : nearbyLocations) {
            System.out.println("Member: " + location.getMemberByString() + ", Distance: " + location.getDistance());
        }
    }
}